@echo off
chcp 65001 >nul

:: 检查JDK是否已安装
where java >nul 2>&1
if %errorlevel% equ 0 (
    echo JDK已安装
    java -version
    goto :end
)

echo JDK未安装，开始安装...

:: 设置JDK版本变量
set JDK_VERSION=17.0.8
set JDK_URL=https://download.oracle.com/java/%JDK_VERSION%/archive/jdk-%JDK_VERSION%_windows-x64_bin.zip

:: 检查jdk.zip是否已存在
if exist jdk.zip (
    echo JDK安装包已存在，跳过下载...
) else (
    :: 下载JDK
    echo 正在下载JDK %JDK_VERSION%...
    powershell -Command "Invoke-WebRequest -Uri '%JDK_URL%' -OutFile 'jdk.zip'"
    if %errorlevel% neq 0 (
        echo 下载失败
        exit /b 1
    )
)

:: 解压JDK
echo 正在解压JDK...
powershell -Command "Expand-Archive -Path 'jdk.zip' -DestinationPath ."
if %errorlevel% neq 0 (
    echo 解压失败
    exit /b 1
)

del jdk.zip

:: 设置环境变量
setx JAVA_HOME "%cd%\jdk-%JDK_VERSION%"
setx PATH "%%PATH%%;%%JAVA_HOME%%\bin"

:: 验证安装
call "%JAVA_HOME%\bin\java.exe" -version
if %errorlevel% neq 0 (
    echo 安装验证失败
    exit /b 1
)

echo JDK %JDK_VERSION% 安装成功

:end
pause